/**
 * 
 */
package cn.jy.erp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.jy.erp.domain.IncomeStacking;



/**
 * @author 刘中斐
 *@date 2018年7月26日 上午10:09:45 
 */
public class DateUtil{
	public static Map<String, Object> getWeekByDate(Date time) throws ParseException {
		Map<String, Object> map=new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
		cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		String imptimeBegin = sdf.format(cal.getTime());
		System.out.println("所在周星期一的日期：" + imptimeBegin);
		cal.add(Calendar.DATE, 6);
		String imptimeEnd = sdf.format(cal.getTime());
		System.out.println("所在周星期日的日期：" + imptimeEnd);
		if (time.getTime()>=sdf.parse(imptimeBegin).getTime()&&cal.getTime().getTime()<=sdf.parse(imptimeEnd).getTime()) {
			map.put("benginDate", imptimeBegin);
			map.put("endDate", imptimeEnd);
			map.put("whereWeek", cal.get(Calendar.WEEK_OF_YEAR));
		}
		return map;
	}
	public static int getWeek(Date time,Date time1) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		int a=cal.get(Calendar.WEEK_OF_YEAR);
		cal.setTime(time1);
		int b=cal.get(Calendar.WEEK_OF_YEAR);


		return a-b;
	}
	public static int getWeek1(Date time) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		int a=cal.get(Calendar.WEEK_OF_YEAR);

		return a;
	}
	public static String  getWeek2(Date time) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		int a=cal.get(Calendar.WEEK_OF_YEAR);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if (a<10) {
			return sdf.format(time).substring(0,4) +":0"+a;
		}else {
			return sdf.format(time).substring(0,4) +":"+a;
		}
		
	}
	/**
	 * 获取当前年
	 * @return
	 */
	public static String getCurrentYear(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy");
		return dfs.format(new Date());
	}
	/**
	 * 获取指定年份的前N年或者后N年
	 * 如果num为 正数，则是获取后 num 年，如果为负数，则为前面num年
	 * @param year ： 格式：yyyy 
	 * @return
	 */
	public static String getThatYear(String year,int num){

		int y = Integer.parseInt(year);

		return (y + num) + "";

	}
	/**
	 * 获取两个集合的差集
	 */
	public static List<String> removeList(List<String> list1,List<String> list2){
		List<String> list = new ArrayList<String>(list1);

		list.removeAll(list2);

		return list;

	}
	/**
	 * 将字符串转换为日期
	 * @param date 格式为：yyyy-MM-dd
	 * @return
	 */
	public static Date parseDate(String date){

		if(date != null && !"".equals(date)){
			if(date.length() > 10){
				date = date.substring(0, 10);
			}
			SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date d = dfs.parse(date);
				return d;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 格式化指定日期，格式：yyyy-MM-dd
	 * @return
	 */
	public static String getNowOnlyDateFomat1(Date date){
		String dfs = new SimpleDateFormat("yyyy-MM-dd").format(date);
		return dfs;
	}
	public static String getThatDate(String date,int num,int type){
		Calendar calendar = Calendar.getInstance();
		Date d = DateUtil.parseDate(date);
		calendar.setTime(d);
		calendar.add(type, num);
		d = calendar.getTime();
		return DateUtil.getNowOnlyDateFomat1(d);
	}
	/**
	 * 获取某年某月的最后一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth1(int year, int month) {     
		Calendar cal = Calendar.getInstance();     
		//设置年份  
		cal.set(Calendar.YEAR, year);  
		//设置月份  
		cal.set(Calendar.MONTH, month-1); 
		//获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DATE);
		//设置日历中月份的最大天数  
		cal.set(Calendar.DAY_OF_MONTH, lastDay);  
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		return sdf.format(cal.getTime());
	}

	/**
	 * 根据具体年份周数获取日期范围
	 * @param year
	 * @param week
	 * @param targetNum
	 * @return
	 */
	public static String getWeekDays(int year,int week,int num){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.WEEK_OF_YEAR, week);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK,cal.getFirstDayOfWeek()); 
		Date date = cal.getTime();
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
		cal.add(Calendar.DAY_OF_WEEK, 6);
		String endDate = sd.format(cal.getTime()); 
		return sd.format(date)+"~"+endDate;
	}
	/**
	 * 返回当前年的周数和第二年的所有周数（2018年从30周开始）
	 * @return
	 */
	public static List<String> getOnlyWeek(){
		int k=30;
		SimpleDateFormat sd=new SimpleDateFormat("yyyy");
		List<String> list=new ArrayList<String>();
		String thisYear = sd.format(new Date());
		int nextYear =Integer.valueOf(thisYear)+1;
		if ("2018".equals(thisYear)) {
			for (int i = k; i <= 52; i++) {
				list.add(thisYear+":"+i);
			}
		}else {
			for (int i = 1; i <= 52; i++) {
				if (i<10) {
					list.add(thisYear+":0"+i);
				}else {
					list.add(thisYear+":"+i);
				}

			}
		}
		for (int i = 1; i <= 52; i++) {
			if (i<10) {
				list.add(nextYear+":0"+i);
			}else {
				list.add(nextYear+":"+i);
			}

		}
		return list;
	}
	/**
	 * 绘制甘特图工具类 、保证数据和周长度相同 
	 *
	 * @param list
	 * @return
	 */

	public static List<IncomeStacking> getListLen(List<IncomeStacking> list){
		List<String> onlyWeek = getOnlyWeek();
		double tmp=0;
		String OBJECT=null;
		String jhyf=null;
		String whereWeek=null;
		
		List<IncomeStacking> tmpList=new ArrayList<IncomeStacking>();
		for (String string : onlyWeek) {
			for (IncomeStacking sta : list) {
				if (sta.getWHEREWEEK().equals(string)) {
					
					OBJECT=sta.getOBJECT();
					jhyf=sta.getJHYF();
					whereWeek=sta.getWHEREWEEK();
					tmp+=Double.valueOf(sta.getJHJE());
				}
			}
			
			
			if (tmp!=0) {
				IncomeStacking income=new IncomeStacking();
				income.setJHJE(String.valueOf(tmp));
				income.setJHYF(jhyf);
				income.setOBJECT(OBJECT);
				income.setWHEREWEEK(whereWeek);
				tmpList.add(income);
			}

			tmp=0;
			
		}	
		List<String> tmpWeek =new ArrayList<String>();
		
		
		for (IncomeStacking income : tmpList) {
			tmpWeek.add(income.getWHEREWEEK());
		}
		onlyWeek.removeAll(tmpWeek);
		
		for (String string : onlyWeek) {
			IncomeStacking incomeStacking=new IncomeStacking();
			incomeStacking.setJHJE("0");
			incomeStacking.setJHYF(null);
			incomeStacking.setNAME(null);
			incomeStacking.setOBJECT(null);
			incomeStacking.setWHEREWEEK(string);
			tmpList.add(incomeStacking);
		}
		Collections.sort(tmpList);
		
		
		
		return tmpList;
		
	}
	
	public static void main(String[] args) throws ParseException{
		
		String time ="2018-10-10";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String week2 = getWeek2(sdf.parse(time));
		System.out.println(week2);
	}
	
}
